<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="文章状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"

        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="标签" prop="tagIds">
        <el-input
          v-model="queryParams.tagIds"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['blog:article:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['blog:article:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="article-content" v-loading="loading">
      <div class="seckill_mod_goods" v-for="(item, index) in articleList" :key="index">
        <el-card shadow="hover" :body-style="{ padding: '0px' }">
          <div class="seckill_mod_goods_link">
            <el-image fit="cover" class="seckill_mod_goods_link_img" :preview-src-list="srcList" :src="item.cover"/>
            <el-tooltip placement="bottom"
                        :content="item.title">
              <h5 class="seckill_mod_goods_title">{{item.title}}</h5>
            </el-tooltip>
            <el-tooltip placement="top" :content="item.descript">
              <p class="seckill_mod_goods_desc">
                {{item.descript}}
              </p>
            </el-tooltip>

            <p class="mate">
              <span class="el-icon-time" style="margin-right: 10px" type="text">{{ dateFormat(item.createTime)}}</span>
              <el-button v-if="item.meta.likes|''" size="mini" icon="el-icon-star-on" type="text">
                {{item.meta.likes}}
              </el-button>
              <el-button v-if="item.meta.comments|''" size="mini" icon="el-icon-chat-dot-round" type="text">
                {{item.meta.comments}}
              </el-button>
              <el-button v-if="item.meta.views|''" size="mini" icon="el-icon-view" type="text">
                {{item.meta.views}}
              </el-button>
            </p>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-price-tag"
              @click="handleUpdate(item)"
              v-hasPermi="['blog:article:edit']"
            >标签
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(item)"
              v-hasPermi="['blog:article:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(item)"
              v-hasPermi="['blog:article:remove']"
            >删除
            </el-button>
          </div>
        </el-card>
      </div>
    </div>

<!--    -->
<!--    注释-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title"  :before-close="handleClose" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" show-word-limit maxlength="50" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <imageUpload v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="描述" prop="descript">
          <el-input v-model="form.descript" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="标签" prop="tagIds">
          <el-input v-model="form.tagIds" placeholder="请输入标签"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listArticle, getArticle, delArticle, addArticle, updateArticle, exportArticle } from '@/api/blog/article'
  import ImageUpload from '@/components/ImageUpload'
  import Editor from '@/components/Editor'

  export default {
    name: 'Article',
    components: {
      ImageUpload,
      Editor
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 文章表格数据
        articleList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          isAsc: 'desc',
          orderByColumn: 'id',
          title: null,
          cover: null,
          descript: null,
          status: null,
          content: null,
          userId: null,
          createTime: null,
          tagIds: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '封面不能为空', trigger: 'blur' }
          ],
          descript: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          tagIds: [
            { required: true, message: '请选择标签', trigger: 'blur' }
          ]
        },
        // 大图预览
        srcList: []
      }
    },
    created() {
      this.getList()
      this.getDicts('blog_status').then(response => {
        this.statusOptions = response.data
      })
    },
    methods: {
      /** 查询文章列表 */
      getList() {
        this.loading = true
        listArticle(this.queryParams).then(response => {
          this.articleList = response.rows
          this.srcList = []
          this.articleList.forEach(
            a => {
              this.srcList.push(a.cover)
            }
          )
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          cover: null,
          descript: null,
          status: '0',
          content: null,
          userId: null,
          createTime: null,
          tagIds: null,
          updateTime: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加文章'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getArticle(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改文章'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateArticle(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addArticle(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除文章编号为"' + ids + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delArticle(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有文章数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.exportLoading = true
          return exportArticle(queryParams)
        }).then(response => {
          this.download(response.msg)
          this.exportLoading = false
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
    }
  }
</script>
<style lang="scss" scoped>

  .seckill_mod_goods {
    float: left;
    margin-left: 10px;
    margin-bottom: 10px;
    height: 100%;
    position: relative;
    background: #fff;
  }


  .seckill_mod_goods_link {
    width: 248px;
    padding: 10%;
    background: #fff;
    display: block;
    text-decoration: none;
    position: relative;
    overflow: hidden;
    text-align: center;
  }

  .seckill_mod_goods_title {
    color: #333;
    margin: 20px -10px 9px;
    text-align: left;
    height: 20px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 19px;
    line-height: 1.21053;
    font-weight: 600;
    letter-spacing: .012em;
  }

  .article-content {
    overflow: hidden;
  }

  .seckill_mod_goods_link_img {
    width: auto;
    height: 250px;
    -webkit-transition: -webkit-transform .3s;
    transition: -webkit-transform .3s;
    -moz-transition: transform .3s, -moz-transform .3s;
    transition: transform .3s;
    transition: transform .3s, -webkit-transform .3s, -moz-transform .3s;
  }

  .seckill_mod_goods_desc {
    margin: 0 -10px;
    height: 21px;
    color: #82848a;
    text-align: left;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-size: 13px;
    line-height: 1.42859;

    letter-spacing: -.016em;
  }

  .mate {
    margin: 0 -9px;
    height: 21px;
    color: #82848a;
    text-align: left;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 12px;
    line-height: 1.42859;
    font-weight: 400;
    letter-spacing: -.016em;
  }
</style>
